<template>
  <div class="main">
    <div class="title">
      <div>
        <span @click="goBackConfig">配置管理</span> > 配置数据:
        {{ configData.configName }}
      </div>
      <el-button type="primary" :icon="CirclePlus" @click="addConfigType"
        >新增配置数据</el-button
      >
    </div>
    <div class="cut-line"></div>
    <el-table
      :data="tabelData"
      height="400"
      style="width: 100%"
      border
      v-loading="loading"
      :header-cell-style="headerStyle"
    >
      <el-table-column type="index" align="center" label="#" width="50" />
      <el-table-column
        prop="configLabel"
        align="center"
        label="配置名称"
        width="200"
      />
      <el-table-column
        prop="configValue"
        align="center"
        label="配置值"
        width="200"
      />
      <el-table-column prop="remark" align="center" label="备注" width="300" />
      <el-table-column align="center" label="操作">
        <template v-slot="scope">
          <div class="table-buts">
            <el-button
              style="background-color: #3d839c; color: #fff"
              @click="updateConfig(scope.row)"
              >修改配置数据</el-button
            >
            <el-button type="danger" @click="deleteConfig(scope.row.id)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加、修改弹窗 -->
    <el-dialog v-model="dialogTableVisible" :title="title" width="300">
      <div class="dialog-win">
        <el-form
          ref="ruleFormRef"
          :model="table"
          label-width="auto"
          :rules="rules"
        >
          <el-form-item label="配置名称：" prop="configLabel">
            <el-input v-model="table.configLabel" style="width: 240px" />
          </el-form-item>
          <el-form-item label="配置值：" prop="configValue">
            <el-input v-model="table.configValue" style="width: 240px" />
          </el-form-item>
          <el-form-item label="备注：" prop="remark">
            <el-input v-model="table.remark" style="width: 240px" />
          </el-form-item>
          <el-form-item>
            <div class="from-but">
              <el-button type="warning" @click="submit(ruleFormRef)"
                >确定</el-button
              >
              <el-button type="info" @click="resetForm(ruleFormRef)"
                >取消</el-button
              >
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
  <script setup>
import { CirclePlus } from "@element-plus/icons-vue";
import message from "@/util/alert.js";
import { ref, reactive, onMounted } from "vue";
import {
  getConfigDataApi,
  addConfigDataApi,
  updateConfigDataApi,
  delelteConfigDataApi,
} from "@/api/configApi.js";
import { useRouter, useRoute } from "vue-router";
import { useConfigStore } from "@/stores";
const configStore = new useConfigStore();
const router = useRouter();
const route = useRoute();
const loading = ref(false);
const tabelData = ref([]);
// 表格头部样式
const headerStyle = () => {
  return {
    backgroundColor: "#a0cfff",
    color: "#000000",
    padding: "8px 0px",
  };
};

// 获取配置列表
const getconfigTabelData = async () => {
  loading.value = true;
  let res = await getConfigDataApi({
    configKey: configData.value.configKey,
  });
  switch (configData.value.configKey) {
    case "system:device:type":
      configStore.deviceTypeList = res.data;
      break;
    case "system:borrow:type":
      configStore.borrowTypeList = res.data;
      break;
    case "book:borrow:type":
      configStore.borrow = res.data;
      break;
  }
  tabelData.value = res.data;
  loading.value = false;
};
// 添加配置类型
const addConfigType = () => {
  table.value = {
    configKey: configData.value.configKey,
    configLabel: "",
    configValue: "",
    remark: "",
  };
  console.log(table.value);
  title.value = "添加配置数据";
  dialogTableVisible.value = true;
};
// 删除配置数据
const deleteConfig = (id) => {
  message.confirm({
    title: "提示",
    msg: `是否删除此配置数据`,
    ok: async () => {
      await delelteConfigDataApi(id);
      message.success(`删除成功`);
      getconfigTabelData();
    },
  });
};
// 修改配置类型信息
const updateConfig = (row) => {
  table.value = JSON.parse(JSON.stringify(row));
  table.value.configId = row.id;
  title.value = "修改配置数据";
  dialogTableVisible.value = true;
};
const goBackConfig = () => {
  router.push("/config");
};
// 弹窗
const table = ref({});
const dialogTableVisible = ref(false);
const title = ref("");
const ruleFormRef = ref(null);
// 添加、修改的规则
const rules = reactive({
  configLabel: [{ required: true, message: "请输入配置名称", trigger: "blur" }],
  configValue: [{ required: true, message: "请输入配置值", trigger: "blur" }],
});
// 提交表单
const submit = async (ruleForm) => {
  if (!ruleForm) return;
  await ruleForm.validate(async (valid, fields) => {
    if (valid) {
      if (title.value == "修改配置数据") {
        await updateConfigDataApi(table.value);
        getconfigTabelData();
        dialogTableVisible.value = false;
        message.success("修改配置数据成功");
      } else if (title.value == "添加配置数据") {
        await addConfigDataApi(table.value);
        getconfigTabelData();
        dialogTableVisible.value = false;
        message.success("添加配置数据成功");
      }
    }
  });
};
// 重置表单
const resetForm = (ruleForm) => {
  ruleForm.resetFields();
  dialogTableVisible.value = false;
  table.value = {};
};
const configData = ref({});
onMounted(() => {
  configData.value = route.query;
  getconfigTabelData();
});
</script>
  <style lang="scss" scoped>
.main {
  width: 100%;
  height: 100%;
  background-color: #ffffff;
  padding: 10px 20px;
  .title {
    font-size: 20px;
    font-weight: 600;
    display: flex;
    justify-content: space-between;
    span:hover {
      color: #79bbff;
      cursor: pointer;
    }
  }
}
.search-bar {
  margin-top: 20px;
  display: flex;
  align-items: center;
  height: 30px;
  .search-btn {
    background-color: #79bbff;
    color: #ffffff;
  }
  .el-button {
    height: 28px;
  }
}
.label-input {
  display: flex;
  color: #aaaaaa;
  height: 30px;
  align-items: center;
  margin-right: 20px;
  font-weight: 550;
  .el-input {
    border: none;
    height: 30px;
  }
}
.cut-line {
  margin-top: 30px;
  height: 1px;
  background-color: #cdd0d6;
}

.pagination {
  display: flex;
  justify-content: center;

  .el-pagination {
    margin: auto 0;
    margin-top: 10px;
  }
}
:deep(.el-table) {
  margin-top: 20px;
  .el-table__cell {
    padding: 4px;
  }
  .cell {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.value-list {
  .value-item {
    display: flex;
    flex-direction: row;
    margin-bottom: 10px;
    .el-input {
      margin-right: 10px;
    }
  }
  .add {
    margin-left: 270px;
    margin-bottom: 10px;
    font-size: 24px;
  }
  .add:hover {
    color: #79bbff;
  }
}
.buts {
  width: 100%;
  display: flex;
  justify-content: center;
}
</style>